﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AccountSummaryViewModelTests.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the AccountSummaryViewModelTests type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Test.ViewModel
{
    using System.Collections.Generic;
    using Business.Interfaces;

    using Hisaab.Windows.Common;

    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Moq;
    using Windows.Client.Model;
    using Windows.Client.ViewModel;
    using AccountSummary = Business.Entities.AccountSummary;

    /// <summary>
    /// Defines the <seealso cref="Hisaab.Client.Test.ViewModel.AccountSummaryViewModelTests"/> type.
    /// </summary>
    [TestClass]
    public class AccountSummaryViewModelTests
    {
        /// <summary>
        /// Constructors the should not throw if account is null.
        /// </summary>
        [TestMethod]
        public void ConstructorShouldNotThrowIfAccountIsNull()
        {
            var mockManager = new Mock<IAccountManager>();
            var mockSession = new Mock<ISession>();
            new AccountSummaryViewModel(null, mockManager.Object, mockSession.Object);
        }

        /// <summary>
        /// Constructors the should not throw if account is null.
        /// </summary>
        [TestMethod]
        public void ConstructorShouldGetAccontSummaryFromAccountmanagerIfAccountIsNotNull()
        {
            var mockManager = new Mock<IAccountManager>();
            var mockSession = new Mock<ISession>();
            mockManager.Setup(x => x.GetAccountSummary(It.IsAny<int>(), It.IsAny<int>())).Returns(new AccountSummary());
            mockSession.SetupGet(x => x.LoggedUser).Returns(15);

            new AccountSummaryViewModel(new Account { AccountId = 45 }, mockManager.Object, mockSession.Object);

            mockManager.Verify(x => x.GetAccountSummary(45, 15), Times.Once());
        }

        /// <summary>
        /// Constructors the should not throw if account is null.
        /// </summary>
        [TestMethod]
        public void ConstructorShouldSetSummaryPropertyIfAccountIsNotNull()
        {
            var mockManager = new Mock<IAccountManager>();
            var mockSession = new Mock<ISession>();
            var accountSummary = new AccountSummary
                                     {
                                         EndingBalance = 45M,
                                         TransactionCount = 1,
                                         ChartData = new[] { new KeyValuePair<string, decimal>("key1", 2.3M) }
                                     };
            mockManager.Setup(x => x.GetAccountSummary(It.IsAny<int>(), 48)).Returns(accountSummary);
            mockSession.SetupGet(x => x.LoggedUser).Returns(48);

            var vm = new AccountSummaryViewModel(new Account { AccountId = 45 }, mockManager.Object, mockSession.Object);

            Assert.AreEqual(accountSummary.EndingBalance, vm.Summary.EndingBalance);
            Assert.AreEqual(accountSummary.TransactionCount, vm.Summary.TransactionCount);
            Assert.AreEqual(accountSummary.ChartData.Length, vm.Summary.ChartData.Length);
            Assert.AreEqual(accountSummary.ChartData[0], vm.Summary.ChartData[0]);
            mockManager.Verify(x => x.GetAccountSummary(45, 48), Times.Once());
        }

        /// <summary>
        /// Summaries the should raise on property changed if new value is changed.
        /// </summary>
        [TestMethod]
        public void SummaryShouldRaiseOnPropertyChangedIfNewValueIsChanged()
        {
            var summaryChanged = false;
            var mockManager = new Mock<IAccountManager>();
            var mockSession = new Mock<ISession>();
            var accountSummary = new Windows.Client.Model.AccountSummary
            {
                EndingBalance = 45M,
                TransactionCount = 1,
                ChartData = new[] { new KeyValuePair<string, decimal>("key1", 2.3M) }
            };

            var vm = new AccountSummaryViewModel(null, mockManager.Object, mockSession.Object);
            vm.PropertyChanged += (s, e) =>
                                      {
                                          if (e.PropertyName == "Summary")
                                          {
                                              summaryChanged = true;
                                          }
                                      };
            vm.Summary = accountSummary;

            Assert.IsTrue(summaryChanged);
        }
    }
}